home *** CD-ROM | disk | FTP | other *** search
- Path: maple.sover.net!mountain
- From: mountain@maple.sover.net (Steve Mount)
- Newsgroups: comp.lang.c
- Subject: Re: Converting Strings to Upper Case
- Date: 19 Mar 1996 16:39:27 GMT
- Organization: SoVerNet, Inc.
- Message-ID: <4imnvv$mu@thrush.sover.net>
- References: <4ifra6$52i@scipio.cyberstore.ca> <4ih7l3$526@thrush.sover.net> <4ihl4m$4ca@castle.nando.net>
- NNTP-Posting-Host: maple.sover.net
- X-Newsreader: TIN [version 1.2 PL2]
-
- Bill McCarthy (actuary@nando.net) wrote:
- : In <4ih7l3$526@thrush.sover.net>, mountain@sover.net (Steve Mount) writes:
- : >In article <4ifra6$52i@scipio.cyberstore.ca>, ejw@news.cyberstore.ca says...
- : >>I need to write a function to convert a string containg upper or lower case
- : >>characters to the opposite case. Something like:
- : >>
- : >> void libConvertUpperCase(char *str); and
- : >> void libConvertLowerCase(char *str);
- : >>
- : >>and the string would be modified. I just can't seem to wrap my head around
- : >>the best way that I know is better than writing a for loop to check each
- : >>element in the array?
- : >
- : >My home compiler has strlwr and strupr functions. At work, we don't, so
- : >just to make it work, quick and dirty, I did:
- : >
- : >void strnlwr(char *buffer, int len)
- : >{
- : >register int i;
- : >for (i=0;i<len;i++) buffer[i] = tolower(buffer[i]);
- : >return;
- : >}
-
- : A couple of comments on this solution. The inclusion of the parameter
- : len is problematic. First the user must obtain a length before the call.
- : Second, not having a check in the function, conversion may continue
- : beyond the end of the string.
-
- In our context, we always know the length. If we wanna be sure, we use
- either with a sizeof call or a strlen call. True, this is not a wonder-
- function - like I said, just something quick and dirty to get the job done.
- I've been working on other solutions for in-house use (I asked for comments
- in this newsgroup on my code... maybe you'll see it later in the topic
- list), but this was code I'd cut and pasted many a time.
-
- : Also the function returns nothing. It is convenient to return a pointer.
-
- Again, in my context, a return value was not needed. My newer version does
- return a pointer to the buffer, just like memcpy and the like.
-
- : Also, char may be signed and should therefore be cast before apply-
- : ing tolower(). Finally, it is always a good idea to declare a function
- : before using it.
-
- I did not include the prototype in my post, but it's in the code.... somewhere.
-
- : #include <ctype.h>
-
- : char *mystr2lwr( char *s )
- : {
- : char *t = s;
-
- : if ( t != NULL )
- : while ( *s )
- : *s++ = tolower( (unsigned char)*s );
-
- : return t;
- : }
-
- This works great on null-terminated strings... but my function needed to work
- on any old buffer.
-
- +============================================================================+
- | Steve Mount, Software Engineer Work: sjjm@hawkeye.idx.com |
- | CIS: 73720,3404 MSN: S_Mountain Home: mountain@sover.net |
- | AOL: Mountain |
- | WWW: http://www.sover.net/~mountain/ "Fight, Win, Prevail!" |
- +============================================================================+
-